#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/21 11:41
# @Author  : 柳晓飞
# @Site    : 
# @File    : table.py
# @Software: PyCharm
import player
import logic
import jjGlobal.jjGlobal as  jjGlobal
import net.protocal as protocal
import json

class Table:
    def __init__(self,tableID):
        self.tableID = tableID
        self.posDict = {}#key=posID(0 1 2 3 4 5) value=player对象
        self.logic = logic.Logic()
        self.logic.customInit()
        self.leftCardList = None
        self.outCards= []
        self.size = None
    def customInit(self):
        pass
    def enterPlayer(self,p):
        for i in range(0,6):
            if not self.posDict.has_key(i):
                self.posDict[i] = p
                p.pos = i
                break

        self.logic.start()
        self.notifyInOutTable(1,p)

    def leavePlayer(self,playerID):
        for i in range(0,6):
            if self.posDict.has_key(i) and self.posDict[i].playerID == playerID:
                self.notifyInOutTable(0, self.posDict[i])
                del self.posDict[i]
                return True
        return False
    def notifyInOutTable(self,jjType ,p):
        for pIter in self.posDict.values():
            if pIter.playerID != p.playerID:
                msg = {}
                msg['id'] = protocal.notifyInOutTable
                msg['jjType'] = jjType
                msg['playerID'] = p.playerID
                msg['pos'] = self.getPosByPlayerID(p.playerID)
                d = json.dumps(msg)
                jjGlobal.gameserver.gameServerHand.sendMsg(pIter.playerID, d)

    def isEixtPlayer(self,playerID):
        for p in self.posDict.values():
            if playerID == p.playerID:
                return True
        return False
    def getPosByPlayerID(self,playerID):
        for (pos, p) in self.posDict.items():
            if playerID==p.playerID:
                return pos
        return None
    def setTableSize(self,size):
        self.size = size
